/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChatClient.java
 *
 * Created on Aug 11, 2013, 9:29:27 AM
 */
package cs137_chatapp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author GON
 */
public class ChatClient extends javax.swing.JFrame implements ActionListener, Runnable {

    /** Creates new form ChatClient */
    public ChatClient() {
        
    }

    BufferedReader br;
    BufferedWriter bw;

    private ChatClient(String name, String ip, int port) {
        super(name);
        initComponents();
        setLocationRelativeTo(null);

        refreshButton.addActionListener((ActionListener) this);
        try{
		socket = new Socket(ip,port);
		br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

		Date det = new Date();
		DateFormat dateF = new SimpleDateFormat("MM/dd/yyyy");
		DateFormat timeF = new SimpleDateFormat("HH:mm:ss");

		bw.write(name);bw.newLine(); bw.flush();
		String date = (name+ " has logged in at "+dateF.format(det)+" "+timeF.format(det));
		bw.write(date); bw.newLine(); bw.flush();
                chatPanel.append(date);
		Thread th;
		th = new Thread(this);
		th.start();
	}catch(Exception e){}
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        chatPanel = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        messageArea = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        StatusText = new javax.swing.JLabel();
        refreshButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Chat Application");
        setBackground(new java.awt.Color(255, 255, 255));
        setLocationByPlatform(true);

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));

        jScrollPane2.setBackground(new java.awt.Color(204, 204, 255));
        jScrollPane2.setEnabled(false);
        jScrollPane2.setFont(new java.awt.Font("Segoe UI Light", 0, 12));

        chatPanel.setFocusable(false);
        jScrollPane2.setViewportView(chatPanel);

        messageArea.setColumns(20);
        messageArea.setRows(3);
        jScrollPane3.setViewportView(messageArea);

        jLabel1.setText("Enter your message here:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jButton1.setFont(new java.awt.Font("Segoe UI Light", 1, 12));
        jButton1.setText("Username");

        jPanel2.setBackground(new java.awt.Color(0, 0, 0));

        StatusText.setFont(new java.awt.Font("Segoe UI Light", 0, 12));
        StatusText.setForeground(new java.awt.Color(255, 255, 255));
        StatusText.setText("Status: Syncing Attachments");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(StatusText, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(StatusText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
        );

        refreshButton.setFont(new java.awt.Font("Segoe UI Light", 1, 12));
        refreshButton.setText("Refresh");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(refreshButton, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(refreshButton))
                .addContainerGap())
        );

        pack();
    }

    /**
     * @param args the command line arguments
     */
    ServerSocket server;
    Socket socket;
    BufferedReader inp;
    String name, serverName;
    private java.awt.List clientsList;
    int clientCount, ctr, type, board[], opponentID, ownID;

    public void acceptConnection(int port){
        Date det = new Date();
        DateFormat dateF = new SimpleDateFormat("MM/dd/yyyy");
        DateFormat timeF = new SimpleDateFormat("HH:mm:ss");
        try {
		server = new ServerSocket(port);
                chatPanel.append("Port "+port+" opened on local machine. "+dateF.format(det)+" "+timeF.format(det));
        }
        catch(IOException e) {
		System.exit(-1);
        }

        while(true) {
		try {
			int i=0;
			String temp="";
			socket = server.accept();
			inp = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			name = inp.readLine();
		}
		catch(IOException ioe) {
			System.exit(-1);
		}
        }
    }

    public static void main(String args[]) {
        int port;
        String name, ip;

        name = JOptionPane.showInputDialog(null, "Enter Chat Name: ");
        ip = JOptionPane.showInputDialog(null, "Enter IP Address: ");
        port = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Port Number: "));
        ChatClient app = new ChatClient(name,ip,port);
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel StatusText;
    private javax.swing.JTextArea chatPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea messageArea;
    private javax.swing.JButton refreshButton;

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void run() {
        String msgg="";
	int opt, i;

	try{
            socket.setSoTimeout(1);
	}catch(Exception e){}
	while (true){
		try{
                    msgg = br.readLine();
		}catch (Exception h){}
	}
    }
    // End of variables declaration
}
